package com.ekuaitu.kuaitustaff.adapter;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.utils.DistanceUtil;
import com.bumptech.glide.Glide;
import com.ekuaitu.kuaitustaff.R;
import com.ekuaitu.kuaitustaff.activity.CarNewMainMapActivity;
import com.ekuaitu.kuaitustaff.application.MyApplication;
import com.ekuaitu.kuaitustaff.bean.CarOrderBean;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Michael Pi on 2016/9/5.
 */
public class CarsPagerAdapter extends PagerAdapter {


    private Context context;
    public List<CarOrderBean.AttachmentBean> list = new ArrayList<CarOrderBean.AttachmentBean>();
    private LayoutInflater inflater = null;

    public CarsPagerAdapter(Context context, List<CarOrderBean.AttachmentBean> carOrderList) {
        this.context = context;
        this.list = carOrderList;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {
        View view = inflater.inflate(R.layout.item_vp_detail, container, false);
        ImageView iv_carimg = (ImageView) view.findViewById(R.id.iv_carimg);
        TextView tv_type = (TextView) view.findViewById(R.id.tv_type);
        TextView tv_car_distance = (TextView) view.findViewById(R.id.tv_car_distance);
        TextView tv_car_duration = (TextView) view.findViewById(R.id.tv_car_duration);
        TextView tv_car_volatage = (TextView) view.findViewById(R.id.tv_car_volatage);
        TextView tv_car_license = (TextView) view.findViewById(R.id.tv_car_license);
        tv_car_license.setText(list.get(position).getLicenseTag());
        tv_car_distance.setText(new DecimalFormat("#.#").format(DistanceUtil.getDistance(CarNewMainMapActivity.curPosition, new LatLng(list.get(position).getLatitude(), list.get(position).getLongitude())) / 1000));
        tv_car_duration.setText(new DecimalFormat("#").format(Double.valueOf(list.get(position).getMileage())));
        tv_car_volatage.setText(list.get(position).getVolatage());
        if (list.get(position).getTaskNameArray().size() != 0) {
            tv_type.setText(list.get(position).getTaskNameArray().get(0));
        }
        Glide.with(context).load(MyApplication.imageUrl + list.get(position).getCarImg()).into(iv_carimg);
        container.addView(view);
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    public void refreshData(List<CarOrderBean.AttachmentBean> data) {
        list.clear();
        list.addAll(data);
        notifyDataSetChanged();
    }

    public CarOrderBean.AttachmentBean getItem(int position) {
        return list.get(position);
    }

}
